﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SplicerTest
{
    using System.Drawing;
    using System.IO;
    using Splicer.Renderer;
    using Splicer.Timeline;
    using Splicer.WindowsMedia;
    using Color = System.Drawing.Color;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // generates a little slide-show, with audio track and fades between images.

            var disk = "HD1";
            string outputFile = "FadeBetweenImages.wmv"; 
            
            RenderOfflineMessage(disk, outputFile);
        }

        private void RenderOfflineMessage(string disk, string outputFile)
        {
            var bg = new SolidBrush(Color.Black);
            var fg = new SolidBrush(Color.White);
            var width = 1280;
            var height = 720;
            var text = "Please insert disk " + disk;
            var font = new Font(System.Drawing.FontFamily.GenericSansSerif, 30);
            var duration = 10;
            
            Bitmap bitmap = new Bitmap(width, height);
            var graf = Graphics.FromImage(bitmap);
            graf.FillRectangle(bg,0,0,width,height);
            var fSize = graf.MeasureString(text, font);
            graf.DrawString(text, font, fg, (width - fSize.Width) / 2, (height - fSize.Height) / 2);
            

            using (ITimeline timeline = new DefaultTimeline())
            {
                IGroup group = timeline.AddVideoGroup(32, width, height);
                
                ITrack videoTrack = group.AddTrack();
                IClip clip1 = videoTrack.AddImage(bitmap, 0, duration); // play first image for a little while
                
                // fade out and back in
                //group.AddTransition(0, 2, StandardTransitions.CreateFade(), false);
                //group.AddTransition(duration - 2, duration, StandardTransitions.CreateFade(), true);
                
                ITrack audioTrack = timeline.AddAudioGroup().AddTrack();
                //IClip audio = audioTrack.AddAudio("testinput.wav", 0, videoTrack.Duration);
                // create an audio envelope effect, this will:
                // fade the audio from 0% to 100% in 1 second.
                // play at full volume until 1 second before the end of the track
                // fade back out to 0% volume
                //audioTrack.AddEffect(0, audio.Duration, StandardEffects.CreateAudioEnvelope(1.0, 1.0, 1.0, audio.Duration));

                // render our slideshow out to a windows media file
                IRenderer renderer = new WindowsMediaRenderer(timeline, outputFile, File.ReadAllText("WMV-HD-1280x720.prx"));
                renderer.Render();
            }
        }
    }
}
